﻿using LeaderCCSLaserUI.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeaderCCSLaserUI.Services
{
    public class GetBarcodeTCPService : IGetBarcodeService
    {
        private string uploaderIP = "127.0.0.1";
        public string[] Barcodes { get; set; }

        public bool GetBarcode()
        {
            try
            {
                var str = MFunc.LazerScript(uploaderIP, "GetBarcode:2", 13001);
                if (str != "Error")
                {
                    var bars = str.Split(new char[] { '|' });
                    if (bars.Length > 0)
                    {
                        Barcodes = new string[bars.Length];
                        for (int i = 0; i < Barcodes.Length; i++)
                        {
                            var strs = bars[i].Split(new char[] { ';' });
                            Barcodes[i] = strs[0];
                        }
                        return true;
                    }
                    else
                    {
                        Barcodes = new string[4] { string.Empty, string.Empty, string.Empty, string.Empty };
                        return false;
                    }
                }
                else
                {
                    Barcodes = new string[4] { string.Empty, string.Empty, string.Empty, string.Empty };
                    return false;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                Barcodes = new string[4] { string.Empty, string.Empty, string.Empty, string.Empty };
                return false;
            }
        }

        public void SetBarcode(string barcode, int product)
        {
            try
            {
                Barcodes[product] = barcode;
            }
            catch { }
        }

        public GetBarcodeTCPService()
        {
            uploaderIP = ConfigurationManager.ConnectionStrings["UpLoaderIP"].ConnectionString;
            Barcodes = new string[4] { string.Empty, string.Empty, string.Empty, string.Empty };
        }
    }
}
